﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class Edit : System.Web.UI.Page
    {

        AppLib.User LoggedUser;


        protected void Page_Load(object sender, EventArgs e)
        {

            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }

            LoggedUser = Common.GetLoggedUser(this);

        }

        private void SetUserInfo()
        {

            LblNick.Text = LoggedUser.Nick;
            TextBoxName.Text = LoggedUser.Name;
            TextBoxSurname.Text = LoggedUser.Surname;
            TextBoxAddress.Text = LoggedUser.Address;
            TextBoxEmailP.Text = LoggedUser.PrimaryEmail;
            TextBoxEmailS.Text = LoggedUser.SecondaryEmail;
            TextBoxPhone.Text = LoggedUser.PhoneNumber;
            TextBoxICQ.Text = LoggedUser.ICQNumber;
            TextBoxURL.Text = LoggedUser.ImageURL;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {

            SetUserInfo();

        }

        protected void LinkButton6_Click(object sender, EventArgs e)
        {

            //potreba kvuli customvalidatoru, validace je serverside, zastavi se odesilani a validator se vypise, protoze neni polozka v poradku
            if (Page.IsValid == false)
                return;

            LoggedUser.Name = TextBoxName.Text;
            LoggedUser.Surname = TextBoxSurname.Text;
            LoggedUser.Address = TextBoxAddress.Text;
            LoggedUser.PhoneNumber = TextBoxPhone.Text;
            LoggedUser.PrimaryEmail = TextBoxEmailP.Text;
            LoggedUser.SecondaryEmail = TextBoxEmailS.Text;
            LoggedUser.ICQNumber = TextBoxICQ.Text;
            LoggedUser.ImageURL = TextBoxURL.Text;


            if (TextBoxPA.Text.Trim() == "" && TextBoxPB.Text.Trim() == "")
                LoggedUser.Password = Common.GetLoggedUser(this).Password;
            else 
            {
                if (TextBoxPA.Text == TextBoxPB.Text)
                {
                    LoggedUser.Password = MidApp.Hasher.PasswordHasher(TextBoxPB.Text);
                }
                else
                {
                    Label1.Text = "Hesla nejsou totožná.";
                }
            }                

            MidApp.ManageUser.AssignNewPersonalInfo(LoggedUser);

            Common.SetLoggedUser(Page, MidApp.ManageUser.GetUserByNick(LoggedUser.Nick));
            SetUserInfo();
           
            Label1.Text = "Změna údajů uložena";
            Label1.Visible = true;
        }
    }
}